﻿/*
* 作者:haojielyb
* 邮箱:haojielyb@qq.com
*/

using Furion.DependencyInjection;
using Lxb.Model;
using SqlSugar;
using System;
using System.Threading.Tasks;

namespace FaceMqttClient

{
    public class Kaoqin_logService : ITransient
    {
        private readonly ISugarQueryable<Kaoqin_log> repository; // 仓储对象：封装简单的CRUD
        private readonly ISugarQueryable<Users> users;
        private readonly ISugarQueryable<Ad> ads;
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        private string path = AppDomain.CurrentDomain.BaseDirectory;

        public Kaoqin_logService(ISqlSugarClient db)
        {
    
            this.db = db;    // 推荐操作
            repository=db.Queryable<Kaoqin_log>();  
            users=db.Queryable<Users>(); 
            ads=db.Queryable<Ad>();
        }

        public Ad getAdbyid(int id) {
            return ads.Where(a => a.id == id).First();
        }
        /// <summary>
        /// remark
        /// </summary>
        /// <returns></returns>
        public async Task AddAsync(Kaoqin_log Kaoqin)
        {
            DateTime dts = DateTime.Now.AddMinutes(-3);
            int ct = repository.Where(kk => kk.cdate > dts && kk.Id == Kaoqin.Id).CountAsync().Result;
            if (ct > 2)
            {
                Kaoqin.state = 1;
            }
            int userid = Convert.ToInt32(Kaoqin.Id);
            try
            {
                var user = users.Where(u => u.user_id == userid).First();
                if (user != null)
                {

                    var dat = DateTime.Now.Date;
                    var ad= ads.Where(a => a.school == user.slname && a.stime <= dat && a.etime >= dat).First();
                    if(ad!=null)
                     Kaoqin.adid = ad.id;
                }

            }
            catch (Exception)
            {


            }

            await db.Insertable(Kaoqin).ExecuteCommandAsync();
        }
    }
}